const http = require('http');
var url = require('url');
var util = require('util');
// 请求2次的原因 是网络访问的2次,一次请求,一次favicon.ico
let server = http.createServer();

server.on("request", (req, res) => {
  res.setHeader("content-type", "application/json;charset=utf-8")
  //从req中获取请求相关内容
  console.log(req.method);
  console.log(req.url);
  if (req.method === "GET"&&req.url.includes("/index.html")) {
    let search = req.url.split("?")[1];
    //获取get请求的参数
    // 创建URLSearchParams对象
    const searchParams = new URLSearchParams(search);
    //解析字符串
    res.write(searchParams.get("name"))
    res.write(util.inspect(url.parse(req.url, true)));
  } 
  res.end();
})

server.listen("8080", () => {
  console.log("创建了项目,监听8080,访问http:127.0.0.1:8080");
})
